﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>测试本地存储</title>
    <script>
		window.onload = function(){
			if(window.localStorage){
				console.log("支持localStorage");
				localStorage.setItem("name","存储的值");
				console.log(localStorage.getItem("name"));
				localStorage.removeItem("name");//删除指定key为"name"的item
				localStorage.setItem("name2","存储的值");
				localStorage.clear();//删除所有key-value
				var userData = {
					name:"XiaoMing",
					account:"SankoYou",
					level:1,
					disable:true
				}
				//存储JSON数据
				localStorage.setItem("uData",JSON.stringify(userData));
				//读取JSON数据
			    var getUserData = JSON.parse(localStorage.getItem("uData"));
				console.log(getUserData);
				//修改JSON
				getUserData.name = "新XiaoMing";
				localStorage.setItem("uData",JSON.stringify(getUserData));
				var getUserData2 = JSON.parse(localStorage.getItem("uData"));
				console.log(getUserData2);
				//删除本地存储的item
				localStorage.removeItem("uData");

			}
			if(window.sessionStorage){
				//其他方法与localStorage相同
				console.log("支持sessionStorage");
				sessionStorage.setItem("name","存储的值");
				console.log(sessionStorage.getItem("name"));
			}
		};
    </script>
</head>
<body>

</body>
</html>